package demo.javax.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * tomcat开启以及关闭的监听器
 * 
 *
 */
public class ServletContextListenerDemo implements ServletContextListener {

    static Logger logger = LoggerFactory.getLogger(ServletContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("contextInitialized");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("tomcat已经关闭！开始关闭quartz！");
        try {
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();// 创建新的调度器工厂
            Scheduler scheduler = schedulerFactory.getScheduler();// 获取当前进程的所有定时器线程数据
            scheduler.shutdown(false);// 关闭定时器线程

            logger.info("关闭定时器线程成功！");
        } catch (Exception e) {
            logger.error("关闭定时器线程失败！", e);
        }

    }

}
